package com.caidan.diagram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caidan.diagram.common.Result;
import com.caidan.diagram.common.TokenUtils;
import com.caidan.diagram.domain.dto.FigureUserLoginDTO;
import com.caidan.diagram.domain.dto.FigureUserRegisterDTO;
import com.caidan.diagram.domain.entity.FigureUser;
import com.caidan.diagram.service.FigureUserService;
import com.caidan.diagram.mapper.FigureUserMapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
* @author 21639
* @description 针对表【figure_user(用户)】的数据库操作Service实现
* @createDate 2025-05-06 17:46:29
*/
@Service
@Slf4j
@AllArgsConstructor
public class FigureUserServiceImpl extends ServiceImpl<FigureUserMapper, FigureUser>
    implements FigureUserService{

    private FigureUserMapper figureUserMapper;

    @Override
    public Result registerUser(FigureUserRegisterDTO figureUserRegisterDTO, HttpServletRequest httpServletRequest) {
        //        1. 验证用户信息是否正确 - 验证码
        if (figureUserRegisterDTO.getUsername() == null || figureUserRegisterDTO.getPassword() == null || figureUserRegisterDTO.getRePassword() == null || figureUserRegisterDTO.getCode() == null) {
            return Result.fail("用户信息填写错误");
        }
//        校验验证码
        String code = (String) httpServletRequest.getSession().getAttribute("code");
        if (!figureUserRegisterDTO.getCode().equals(code)) {
            return Result.fail("验证码错误");
        }
//        2. 查询数据库中是否存在这个用户
        FigureUser figureUser = figureUserMapper.selectOne(new LambdaQueryWrapper<FigureUser>().eq(FigureUser::getUsername, figureUserRegisterDTO.getUsername()).or().eq(FigureUser::getEmail,figureUserRegisterDTO.getEmail()));
        if (figureUser != null) {
            return Result.fail("用户信息已注册");
        }
        if (!figureUserRegisterDTO.getPassword().equals(figureUserRegisterDTO.getRePassword())) {
            return Result.fail("两次密码不一致");
        }
        FigureUser figureUser1 = new FigureUser();
        figureUser1.setUsername(figureUserRegisterDTO.getUsername());
        figureUser1.setPassword(figureUserRegisterDTO.getPassword());
        figureUser1.setCreateTime(new Date());
        figureUser1.setUpdateTime(new Date());
        figureUser1.setIsDelete(0);
        figureUser1.setStatus(0);
        figureUser1.setEmail(figureUserRegisterDTO.getEmail());
        figureUser1.setAvatar("https://picsum.photos/200/200");
        figureUser1.setNickname(figureUserRegisterDTO.getNickname());
//        普通用户

        int insert = figureUserMapper.insert(figureUser1);
        if (insert < 0) {
            return Result.ok("注册失败");
        }
        // 生成token并返回
        String token = TokenUtils.encodeBase64(figureUserRegisterDTO.getUsername());
        return Result.ok(token);
    }

    @Override
    public Result login(FigureUserLoginDTO figureUserLoginDTO, HttpServletRequest httpServletRequest) {
        if (figureUserLoginDTO.getUsername() == null || figureUserLoginDTO.getPassword() == null) {
            return Result.fail("用户信息填写错误");
        }
//        校验验证码
        String code = (String) httpServletRequest.getSession().getAttribute("code");
        if (!figureUserLoginDTO.getCode().equals(code)) {
            return Result.fail("验证码错误");
        }
        FigureUser figureUser = figureUserMapper.selectOne(new LambdaQueryWrapper<FigureUser>().eq(FigureUser::getUsername, figureUserLoginDTO.getUsername()));
        if (figureUser == null) {
            return Result.fail("登录信息有误");
        }
//        校验密码
        if (!figureUser.getPassword().equals(figureUserLoginDTO.getPassword())) {
            return Result.fail("登录信息有误");
        }
        // 生成token并返回
        String token = TokenUtils.encodeBase64(figureUserLoginDTO.getUsername());
        return Result.ok(token);
    }
}




